#include "funwindow.h"
#include "ui_funwindow.h"
#include "choicewindow.h"

funWindow::funWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::funWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("功能");
    advTextList << "欢迎广告商入驻！！"<<"广告一"<<"广告二"<<"广告三";
    videoList <<"./1.mp4"<<"./2.mp4"<<"./3.mp4";
    textIndex = 0;
    videoIndex = 0;

    process = new QProcess;
    QString cmd = QString("mplayer -x %1 -y %2 -zoom ./1.mp4")
            .arg(ui->videolabel->width())
            .arg(ui->videolabel->height());
    process->start(cmd);
    //为了能够实现自动视频广告播放，建立与信号:finish
    //void finished(int exitCode, QProcess::ExitStatus exitStatus) -- 该信号是一个重载函数
    //所以在使用时，要使用QOverload<信号参数列表>::of(&类名::信号名)
    connect(process,QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),this,&funWindow::auto_play);
    textTimer = new QTimer;
    connect(textTimer,&QTimer::timeout,this,&funWindow::auto_text);
    textTimer->start(3000);


    //创建网络请求管理者
    manager = new QNetworkAccessManager;

    //当网络请求成功后，该管理者对象会发射信号：void finished(QNetworkReply *reply)

    connect(manager,&QNetworkAccessManager::finished,this,&funWindow::read_data);
    //发起网络请求--get
    QUrl url("http://t.weather.itboy.net/api/weather/city/101280101");
    QNetworkRequest request(url);
    manager->get(request);
}

funWindow::~funWindow()
{
    delete ui;
}

void funWindow::auto_play(int exitCode, QProcess::ExitStatus exitStatus)
{

    if(videoIndex == videoList.size())
    {
        videoIndex = 0;
    }

    //播放视频
    QString cmd = QString("mplayer -x %1 -y %2 -zoom %3")
            .arg(ui->videolabel->width())
            .arg(ui->videolabel->height())
            .arg(videoList[videoIndex++]);
    qDebug() << cmd;
    process->start(cmd);
}

void funWindow::auto_text()
{
    if(textIndex == advTextList.size())
    {
        textIndex = 0;
    }
    ui->textlabel->setText(advTextList[textIndex++]);
}

void funWindow::read_data(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();

    //数据解析：第一步：将QByteArray转换成QJSonDocment 第二步：根据数据格式从外向里进行解析
    QJsonDocument doc = QJsonDocument::fromJson(array);
    QJsonObject object =  doc.object();
    QJsonObject cityInfoObject = object.value("cityInfo").toObject();
    QString city = cityInfoObject.value("city").toString();
    ui->citylabel->setText(city);
    QJsonObject dataObject = object.value("data").toObject();
    QJsonArray forecastArray = dataObject.value("forecast").toArray();
    //获取当天的天气
    QJsonObject arrayObejct = forecastArray.at(0).toObject();
    ui->ymdlabel->setText( arrayObejct.value("ymd").toString());
    ui->weeklabel->setText(arrayObejct.value("week").toString());
    ui->highlabel->setText(arrayObejct.value("high").toString());
    ui->lowlabel->setText(arrayObejct.value("low").toString());
}

void funWindow::on_choiceButton_clicked()
{
    choiceWindow * cw = new choiceWindow;
    cw->show();
    this->hide();
}
